//
//  LCZnextColumnController.m
//  PersonalNews
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZnextColumnController.h"
#import "LCZdetailColumnCell.h"
#import "LCZLastColumnController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface LCZnextColumnController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)LCZBaseImageView *headImageView;
@property (nonatomic, strong)LCZBaseLabel *headLabel;

@property (nonatomic, strong)LCZBaseTableView *tableView;

@property (nonatomic, strong)NSMutableArray *allArr;

@property (nonatomic, assign)NSInteger page;

@property (nonatomic, copy)NSString *url;
@end

@implementation LCZnextColumnController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.navigationController.navigationBarHidden = YES;
//
    self.view.backgroundColor = [UIColor colorWithRed:1 green:0.81 blue:0.86 alpha:1];
    // 初始化
    self.allArr = [NSMutableArray array];
    self.page = 0;

    // 创建详情页的头
    self.headImageView = [[LCZBaseImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
    self.headImageView.image = [UIImage imageNamed:@"column1.png"];
    [self.view addSubview:self.headImageView];
    
    LCZBaseView *smallView = [[LCZBaseView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
    [self.view addSubview:smallView];
    smallView.backgroundColor = [UIColor blackColor];
    smallView.alpha = 0.4;
    
    self.headLabel = [[LCZBaseLabel alloc]initWithFrame:CGRectMake((WIDTH - 180) / 2, 20, 180, 80 - 20)];
    self.headLabel.textColor = [UIColor whiteColor];
    // 字体加粗
    self.headLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [self.view addSubview:self.headLabel];
    self.headLabel.textAlignment = NSTextAlignmentCenter;
    
    // 最上面的返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(15, 30, 30, 30);
    backButton.layer.cornerRadius = 15;
    
    backButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor colorWithRed:0.45 green:0.46 blue:0.46 alpha:1]];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];

    
    self.tableView = [[LCZBaseTableView alloc]initWithFrame:CGRectMake(0, 80, WIDTH, HEIGHT - 80) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    
    
    
    [self createData];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
}
// 返回按钮的实现方法
- (void)backAction:(UIButton *)button
{
    // 模态回去 没有NavigationController 让self跳转
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// 请求数据
- (void)createData
{
    NSString *strURL = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/0-20.html", self.tid];
    [LCZNetWorkTools getdataWithString:strURL block:^(id block) {
        NSDictionary *dic = block;
        NSMutableArray *arr = dic[self.tid];
        for (NSMutableDictionary *dic in arr) {
            [self.allArr addObject:dic];
        }
        [self.tableView reloadData];
    }];
}

- (void)footerAction
{
    self.page = self.page + 20;
    NSString *strURL = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/%ld-20.html", self.tid, self.page];
    [LCZNetWorkTools getdataWithString:strURL block:^(id block) {
        NSDictionary *dic = block;
        NSMutableArray *arr = dic[self.tid];
        for (NSMutableDictionary *dic in arr) {
            [self.allArr addObject:dic];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        
    }];
}

#pragma mark tableView的协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    LCZdetailColumnCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[LCZdetailColumnCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.titleLabel.text = self.allArr[indexPath.row][@"title"];
    cell.titleLabel.numberOfLines = 0;
    
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.allArr[indexPath.row][@"imgsrc"]]];
    cell.fromLabel.text = self.allArr[0][@"tname"];

    // 给属性赋值
    self.headLabel.text = self.allArr[0][@"tname"];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allArr.count;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCZLastColumnController *lastController = [[LCZLastColumnController alloc]init];
    lastController.docid = self.allArr[indexPath.row][@"docid"];
    [self presentViewController:lastController animated:YES completion:^{
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
